<?php

include 'picture.php';
Header("Content-type: image/jpeg");

// очищаем файлы кеша старшие 15 минут
if($opendir = opendir('cache'))
{ 
    while(($file = readdir($opendir)) !== false)
	{ 
        if($file != "." && $file != "..")
		{ 
            if(filemtime('cache/' . $file) + 900 < time())
			{
				unlink('cache/' . $file); 
			}
        } 
    } 
    closedir($opendir);  
}  

// Если есть в кеше и не старше 3 минут то грузим из кеша
if(file_exists('cache/' . md5($_SERVER['REQUEST_URI']) . '.jpg') && !array_key_exists('generate', $_REQUEST))
{
	if(filemtime('cache/' . md5($_SERVER['REQUEST_URI']) . '.jpg') + 180 > time())
	{
		$picture = new Picture('cache/' . md5($_SERVER['REQUEST_URI']) . '.jpg');
		$picture->ShowImage(100);
		$picture->DestroyImage();
		exit;
	}
}

// парсим урл
$params = preg_split('#(\/+)#', $_SERVER['REQUEST_URI']);
if($params[1] == 'gamer')
{
	$_REQUEST['type'] = 'gamer';
	$_REQUEST['bg'] = $params[2];
	$_REQUEST['mainfield'] = $params[3];
	$_REQUEST['show_gamer_name'] = $params[4];
	$_REQUEST['show_gamer_place'] = $params[5];
	$_REQUEST['show_gamer_level'] = $params[6];
	$_REQUEST['show_gamer_posts'] = $params[7];
	$_REQUEST['show_gamer_diamond'] = $params[8];
	$_REQUEST['show_gamer_golden'] = $params[9];
	$_REQUEST['show_gamer_silver'] = $params[10];
	$_REQUEST['show_gamer_comments'] = $params[11];
	
	$_REQUEST['gamer_name_x'] = $params[12];
	$_REQUEST['gamer_name_y'] = $params[13];
	$_REQUEST['gamer_place_x'] = $params[14];
	$_REQUEST['gamer_place_y'] = $params[15];
	$_REQUEST['gamer_level_x'] = $params[16];
	$_REQUEST['gamer_level_y'] = $params[17];
	$_REQUEST['gamer_posts_x'] = $params[18];
	$_REQUEST['gamer_posts_y'] = $params[19];
	$_REQUEST['gamer_diamond_x'] = $params[20];
	$_REQUEST['gamer_diamond_y'] = $params[21];
	$_REQUEST['gamer_golden_x'] = $params[22];
	$_REQUEST['gamer_golden_y'] = $params[23];
	$_REQUEST['gamer_silver_x'] = $params[24];
	$_REQUEST['gamer_silver_y'] = $params[25];
	$_REQUEST['gamer_comments_x'] = $params[26];
	$_REQUEST['gamer_comments_y'] = $params[27];
	$_REQUEST['color_r'] = $params[28];
	$_REQUEST['color_g'] = $params[29];
	$_REQUEST['color_b'] = $params[30];
}
else if($params[1] == 'blog')
{
	$_REQUEST['type'] = 'blog';
	$_REQUEST['bg'] = $params[2];
	$_REQUEST['mainfield'] = $params[3];	
	$_REQUEST['show_blog_name'] = $params[4];
	$_REQUEST['show_blog_place'] = $params[5];
	$_REQUEST['show_blog_follow'] = $params[6];
	$_REQUEST['show_blog_diamond'] = $params[7];
	$_REQUEST['show_blog_golden'] = $params[8];
	$_REQUEST['show_blog_silver'] = $params[9];
	$_REQUEST['show_blog_recommend'] = $params[10];	
	
	$_REQUEST['blog_name_x'] = $params[11];
	$_REQUEST['blog_name_y'] = $params[12];	
	$_REQUEST['blog_place_x'] = $params[13];
	$_REQUEST['blog_place_y'] = $params[14];	
	$_REQUEST['blog_follow_x'] = $params[15];
	$_REQUEST['blog_follow_y'] = $params[16];	
	$_REQUEST['blog_diamond_x'] = $params[17];
	$_REQUEST['blog_diamond_y'] = $params[18];	
	$_REQUEST['blog_golden_x'] = $params[19];
	$_REQUEST['blog_golden_y'] = $params[20];	
	$_REQUEST['blog_silver_x'] = $params[21];
	$_REQUEST['blog_silver_y'] = $params[22];	
	$_REQUEST['blog_recommend_x'] = $params[23];
	$_REQUEST['blog_recommend_y'] = $params[24];	
	$_REQUEST['color_r'] = $params[25];
	$_REQUEST['color_g'] = $params[26];
	$_REQUEST['color_b'] = $params[27];	
}


// го го го
if($_REQUEST['type'] == 'gamer')
{
	include 'gamer.php';
	$gamer = new Gamer($_REQUEST['mainfield']);
	$picture = new Picture('bars/bigbar' . $_REQUEST['bg'] . '.png');
	$picture->AddColor('alias', $_REQUEST['color_r'], $_REQUEST['color_g'], $_REQUEST['color_b']);
	
	if($_REQUEST['show_gamer_name']) $picture->DrawString($gamer->GetName(), $_REQUEST['gamer_name_x'], $_REQUEST['gamer_name_y'], 'alias');
	if($_REQUEST['show_gamer_level']) $picture->DrawString($gamer->GetLevel() . " уровень", $_REQUEST['gamer_level_x'], $_REQUEST['gamer_level_y'], 'default');
	if($_REQUEST['show_gamer_posts']) $picture->DrawString("П: " . $gamer->GetPosts('default'), $_REQUEST['gamer_posts_x'], $_REQUEST['gamer_posts_y'], 'default');
	if($_REQUEST['show_gamer_comments']) $picture->DrawString("K: " . $gamer->GetComments(), $_REQUEST['gamer_comments_x'], $_REQUEST['gamer_comments_y'], 'default');

	// место в топе геймеров
	if($_REQUEST['show_gamer_place'])
	{
		if($gamer->GetPlace() == -1)
		{
			$picture->DrawString("администрация", $_REQUEST['gamer_place_x'], $_REQUEST['gamer_place_y'], 'admin');
		}
		else
		{
			if($gamer->GetPlace() == -1) { $color = 'notop'; }
			else if($gamer->GetPlace() <= 10) { $color = 'top10'; }
			else if($gamer->GetPlace() <= 50 && $gamer->GetPlace() > 10) { $color = 'top50'; }
			else if($gamer->GetPlace() <= 100 && $gamer->GetPlace() > 50) { $color = 'top100'; }
			else { $color = 'default'; }
			$picture->DrawString($gamer->GetPlace() . " место в топе", $_REQUEST['gamer_place_x'], $_REQUEST['gamer_place_y'], $color);
		}
	}
	
	// брилиантовые посты
	if($_REQUEST['show_gamer_diamond']) 
	{
		$diamond = imagecreatefrompng("images/b_diamond.png");
		$picture->DrawImage($diamond, $_REQUEST['gamer_diamond_x'], $_REQUEST['gamer_diamond_y'], 0, 0, 5, 5);
		$picture->DrawString($gamer->GetPosts('diamond'), $_REQUEST['gamer_diamond_x'] + 7, $_REQUEST['gamer_diamond_y'], 'diamond');
		imagedestroy($diamond);
	}
	
	// золотые посты
	if($_REQUEST['show_gamer_golden']) 
	{
		$golden = imagecreatefrompng("images/b_golden.png");
		$picture->DrawImage($golden, $_REQUEST['gamer_golden_x'], $_REQUEST['gamer_golden_y'], 0, 0, 5, 5);
		$picture->DrawString($gamer->GetPosts('golden'), $_REQUEST['gamer_golden_x'] + 7, $_REQUEST['gamer_golden_y'], 'golden');
		imagedestroy($golden);
	}
	
	// серебреные посты
	if($_REQUEST['show_gamer_silver']) 
	{
		$silver = imagecreatefrompng("images/b_silver.png");
		$picture->DrawImage($silver, $_REQUEST['gamer_silver_x'], $_REQUEST['gamer_silver_y'], 0, 0, 5, 5);	
		$picture->DrawString($gamer->GetPosts('silver'), $_REQUEST['gamer_silver_x'] + 7, $_REQUEST['gamer_silver_y'], 'silver');
		imagedestroy($silver);
	}

}
else if($_REQUEST['type'] == 'blog')
{
	include 'blog.php';
	$blog = new Blog($_REQUEST['mainfield']);
	$picture = new Picture('bars/bigbar' . $_REQUEST['bg'] . '.png');	
	$picture->AddColor('alias', $_REQUEST['color_r'], $_REQUEST['color_g'], $_REQUEST['color_b']);
	
	if($_REQUEST['show_blog_name']) $picture->DrawString($blog->GetName(), $_REQUEST['blog_name_x'], $_REQUEST['blog_name_y'], 'alias');
	if($_REQUEST['show_blog_follow']) $picture->DrawString($blog->GetFollow(), $_REQUEST['blog_follow_x'], $_REQUEST['blog_follow_y'], 'default');

	// место в топе геймеров
	if($_REQUEST['show_blog_place'])
	{
		if($blog->GetPlace() == -1)
		{
			$picture->DrawString("Не в топе", $_REQUEST['blog_place_x'], $_REQUEST['blog_place_y'], 'notop');
		}
		else
		{
			if($blog->GetPlace() <= 10) { $color = 'top10'; }
			else if($blog->GetPlace() <= 50 && $blog->GetPlace() > 10) { $color = 'top50'; }
			else if($blog->GetPlace() <= 100 && $blog->GetPlace() > 50) { $color = 'top100'; }
			else { $color = 'default'; }
			$picture->DrawString($blog->GetPlace() . " место в топе игр", $_REQUEST['blog_place_x'], $_REQUEST['blog_place_y'], $color);
		}			
	}	
	
	// брилиантовые посты
	if($_REQUEST['show_blog_diamond']) 
	{
		$diamond = imagecreatefrompng("images/b_diamond.png");
		$picture->DrawImage($diamond, $_REQUEST['blog_diamond_x'], $_REQUEST['blog_diamond_y'], 0, 0, 5, 5);
		$picture->DrawString($blog->GetPosts('diamond'), $_REQUEST['blog_diamond_x'] + 7, $_REQUEST['blog_diamond_y'], 'diamond');
		imagedestroy($diamond);
	}
	
	// золотые посты
	if($_REQUEST['show_blog_golden']) 
	{
		$golden = imagecreatefrompng("images/b_golden.png");
		$picture->DrawImage($golden, $_REQUEST['blog_golden_x'], $_REQUEST['blog_golden_y'], 0, 0, 5, 5);
		$picture->DrawString($blog->GetPosts('golden'), $_REQUEST['blog_golden_x'] + 7, $_REQUEST['blog_golden_y'], 'golden');
		imagedestroy($golden);
	}
	
	// серебреные посты
	if($_REQUEST['show_blog_silver']) 
	{
		$silver = imagecreatefrompng("images/b_silver.png");
		$picture->DrawImage($silver, $_REQUEST['blog_silver_x'], $_REQUEST['blog_silver_y'], 0, 0, 5, 5);	
		$picture->DrawString($blog->GetPosts('silver'), $_REQUEST['blog_silver_x'] + 7, $_REQUEST['blog_silver_y'], 'silver');
		imagedestroy($silver);
	}	
	
	// рекомендованные посты
	if($_REQUEST['show_blog_recommend']) 
	{
		$recommend = imagecreatefrompng("images/b_recommend.png");
		$picture->DrawImage($recommend, $_REQUEST['blog_recommend_x'], $_REQUEST['blog_recommend_y'], 0, 0, 5, 5);	
		$picture->DrawString($blog->GetPosts('recommend'), $_REQUEST['blog_recommend_x'] + 7, $_REQUEST['blog_recommend_y'], 'recommend');
		imagedestroy($recommend);
	}		
}

$picture->SaveImage('cache/' . md5($_SERVER['REQUEST_URI']) . '.jpg', 100);
$picture->ShowImage(100);
$picture->DestroyImage();

?>